import axios from "axios";
import { md5Text } from "../utils/md5";

//------------------------------------
const base = process.env.VUE_APP_API;
const API = process.env.VUE_APP_ENV === "dev" ? "/api" : "";
const http = axios.create({
  baseURL: base,
  timeout: 600000,
});

// 请求拦截器
http.interceptors.request.use(
  (config) => {
    config.headers["SIGNATURE"] = md5Text(config.data);
    config.headers["AUTHORIZATION"] = localStorage.getItem("TOKEN");
    config.headers["appMark"] = "cash";
    config.headers["Accept-Language"] =
      localStorage.getItem("accept_language") || "en";
    config.headers["deviceType"] = "h5";
    config.headers["CHANNEL"] = "TT1003";
    config.headers["PROJECT"] = "H5";
    // if (localStorage.getItem("accept_language") == "pt") {
    // }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 响应拦截器 1
http.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (err) => {
    if (err.toString().indexOf("timeout") > -1) {
      console.log("Request Timeout：");
    }
    return Promise.resolve(err);
  }
);

export default {
  get(url, params, headers) {
    return http({
      method: "get",
      url: `${base}${API}${url}`,
      params: params,
      headers: headers,
    });
  },
  post(url, data, options) {
    return http({
      method: "post",
      url: `${base}${API}${url}`,
      data: data,
      ...options,
    });
  },
  // getMarketsUSDT() {
  //     return http({
  //         method: 'get',
  //         url: `https://ftx.com/api/markets/USDT/USD`,
  //     })
  // }
};
